package com.example.lottery.service.impl;

import com.example.lottery.controller.param.CreatePrizeParam;
import com.example.lottery.controller.param.PageParam;
import com.example.lottery.dao.dataobject.PrizeDO;
import com.example.lottery.dao.mapper.PrizeMapper;
import com.example.lottery.service.PictureService;
import com.example.lottery.service.PrizeService;
import com.example.lottery.service.dto.PageListDTO;
import com.example.lottery.service.dto.PrizeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class PrizeServiceImpl implements PrizeService {
    @Autowired
    private PictureService pictureService;

    @Autowired
    private PrizeMapper prizeMapper;

    @Override
    public Long createPrize(CreatePrizeParam param, MultipartFile picFile) {

        //上传图片
        String fileName = pictureService.savePicture(picFile);

        //存库
        PrizeDO prize = new PrizeDO();
        prize.setName(param.getPrizeName());
        prize.setDescription(param.getDescription());

        prize.setImageUrl(fileName);
        prize.setPrice(param.getPrice());
        prizeMapper.insert(prize);
        return prize.getId();

    }

    @Override
    public PageListDTO<PrizeDTO> findPrizeList(PageParam param) {
        //总量
        int total = prizeMapper.count();

        //查询当前页列表
        List<PrizeDO> prizeDOList = prizeMapper.selectPrizeList(param.offset(),param.getPageSize());

        List<PrizeDTO> prizeDTOList = new ArrayList<>();
        for (PrizeDO prizeDO : prizeDOList) {
            PrizeDTO prizeDTO = new PrizeDTO();
            prizeDTO.setDescription(prizeDO.getDescription());
            prizeDTO.setPrice(prizeDO.getPrice());
            prizeDTO.setName(prizeDO.getName());
            prizeDTO.setImageUrl(prizeDO.getImageUrl());
            prizeDTO.setPrizeId(prizeDO.getId());
            prizeDTOList.add(prizeDTO);
        }

        return new PageListDTO<>(total, prizeDTOList);
    }
}
